package dao.imp;

import dao.DaoLogic;
import javabean.Admin;
import javabean.House;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;
import tools.Dbutils;
import ui.Show;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import static ui.Show.view;

//数据访问层
public class DaoLogicImp {
    Scanner scanner = new Scanner(System.in);
    private QueryRunner queryRunner = new QueryRunner();

    public Admin isAdmin(String user,String password){

           /* Scanner scanner = new Scanner(System.in);
            System.out.println("--------------请登录--------------");
            System.out.println("账户：");//aaa
            String user = scanner.next();
            System.out.println("密码：");//123456
            String password = scanner.next();
            String captcha = Dbutils.captcha();
            System.out.println(captcha);
            System.out.println("验证码：");
            String next = scanner.next();*/
            Dbutils.log();
            SqlSession sqlSession = Dbutils.getsqlSession();
            DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
            Admin admin1 = new Admin();
            admin1.setUser(user);
            admin1.setPassword(password);
            Admin admin = mapper.isAdmin(admin1);
            return admin;
        }

    /**
     * 1.新增房源
     * @param
     */

    public void addDao() {
        System.out.println("---请输入你要添加的房屋信息---");
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入电话：");
        String tel = scanner.next();
        System.out.println("请输入地址：");
        String address = scanner.next();
        System.out.println("请输入月租");
        String rent = scanner.next();
        System.out.println("请输入状态");
        String state = scanner.next();
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("name",name);
        stringObjectHashMap.put("tel",tel);
        stringObjectHashMap.put("address",address);
        stringObjectHashMap.put("rent",rent);
        stringObjectHashMap.put("state",state);
        Dbutils.log();
        SqlSession sqlSession = Dbutils.getsqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        System.out.println("stringObjectHashMap = " + stringObjectHashMap);
        mapper.addDao(stringObjectHashMap);
        sqlSession.commit();
    }

    /**
     *2.查找房屋信息
     * @param
     * @param
     * @return
     */
    public House findDao(int id) {
        Dbutils.log();
        SqlSession sqlSession = Dbutils.getsqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        House house=mapper.findDao(id);
        sqlSession.commit();
        if(house==null){
           return null;
        }
        System.out.println("该房屋的信息是："+house);
        return house;
    }


    /**
     * 3.删除房屋信息
     * @param
     * @param
     * @return
     */
    public Integer delDao(int id) {
        Dbutils.log();
        SqlSession sqlSession = Dbutils.getsqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        mapper.delDao(id);
        sqlSession.commit();
        return 1;
    }

    /**
     * 4.修改房屋信息
     * @param
     * @param
     */
    public void updateDao(int id) {
        Dbutils.log();
        SqlSession sqlSession = Dbutils.getsqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        House house = mapper.findDao(id);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("id",id);
        System.out.println("请输入要更改的姓名：");
        String name = scanner.nextLine();
        if(name.equals("")){
            objectObjectHashMap.put("name",house.getName());
        }else {
            objectObjectHashMap.put("name",name);
        }

        System.out.println("请输入要更改的电话：");
        String tel = scanner.nextLine();
        if(tel.equals("")){
            objectObjectHashMap.put("tel",house.getTel());
        }else {
            objectObjectHashMap.put("tel",tel);
        }

        System.out.println("请输入要更改的房屋地址：");
        String address = scanner.nextLine();
        if(address.equals("")){
            objectObjectHashMap.put("address",house.getAddress());
        }else {
            objectObjectHashMap.put("address",address);
        }

        System.out.println("请输入要更改的房屋月租：");
        String rent = scanner.nextLine();
        if(rent.equals("")){
            objectObjectHashMap.put("rent",house.getRent());
        }else {
            objectObjectHashMap.put("rent",rent);
        }

        System.out.println("请输入要更改的房屋状态：");
        String state = scanner.nextLine();
        if(state.equals("")){
            objectObjectHashMap.put("state",house.getState());
        }else {
            objectObjectHashMap.put("state",state);
        }
        Integer integer = mapper.updateDao(objectObjectHashMap);
        sqlSession.commit();
            if(integer>0){
                System.out.println("---成功修改该房源信息---");
            }else{
                System.out.println("修改失败！");
            }
    }
    /**
     * 5.房屋列表
     * @param
     */
    @Test
    public void listDao() {//len是有效长度
        Dbutils.log();
        SqlSession sqlSession = Dbutils.getsqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        List<House> houses = mapper.listDao();
        System.out.println(houses);

        System.out.println("编号"+"\t\t"+"姓名"+"\t\t"+"电话"+"\t\t"+"地址"+"\t\t"+"月租"+"\t\t"+"状态");
        for (int i = 0; i < houses.size(); i++) {
            System.out.println(houses.get(i).getId()+"\t\t"+houses.get(i).getName()+"\t\t"
                    +houses.get(i).getTel()+"\t"+houses.get(i).getAddress()+"\t\t"+houses.get(i).getRent()
                    +"\t\t"+houses.get(i).getState());
        }
    }
}
